package com.tencreat.mall.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallFeedback;
import com.tencreat.domain.mall.vo.FeedBackStatisticsVO;
import com.tencreat.service.mall.IMallFeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 意见反馈Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="意见反馈api")
@RestController
@RequestMapping("/mall/feedback")
public class AdminMallFeedbackController extends BaseController {
    @Autowired
    private IMallFeedbackService mallFeedbackService;

    /**
     * 查询意见反馈列表
     */
    @ApiOperation(value="查询意见反馈列表")
    @PreAuthorize("@ss.hasPermi('mall:feedback:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallFeedback>> list(@RequestBody  MallFeedback mallFeedback) {
        LambdaQueryWrapper<MallFeedback> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(mallFeedback.getMobile())) {
            wrapper.eq(MallFeedback::getMobile, mallFeedback.getMobile());
        }
        if (mallFeedback.getStatus() != null) {
            wrapper.eq(MallFeedback::getStatus, mallFeedback.getStatus());
        }
        if (mallFeedback.getUserId() != null) {
            wrapper.eq(MallFeedback::getUserId, mallFeedback.getUserId());
        }
        if (mallFeedback.getCreateTime() != null) {
            wrapper.gt(MallFeedback::getCreateTime, mallFeedback.getCreateTime());
        }
        if (mallFeedback.getUpdateTime() != null) {
            wrapper.gt(MallFeedback::getUpdateTime, mallFeedback.getUpdateTime());
        }
        if (mallFeedback.getEndTime() != null) {
            wrapper.lt(MallFeedback::getCreateTime, mallFeedback.getEndTime());
        }
        if (mallFeedback.getStartTime() != null) {
            wrapper.gt(MallFeedback::getCreateTime, mallFeedback.getStartTime());
        }
        if (mallFeedback.getReplyEndTime() != null) {
            wrapper.lt(MallFeedback::getReplyTime, mallFeedback.getReplyEndTime());
        }
        if (mallFeedback.getReplyStartTime() != null) {
            wrapper.gt(MallFeedback::getReplyTime, mallFeedback.getReplyStartTime());
        }
        wrapper.orderByDesc(MallFeedback::getCreateTime);
        Page<MallFeedback> page =  new Page<>(mallFeedback.getPageNum(), mallFeedback.getPageSize());
        return HttpResult.ok(mallFeedbackService.page(page, wrapper));
    }

    /**
     * 导出意见反馈列表
     */
    @ApiOperation(value="导出意见反馈列表")
    @PreAuthorize("@ss.hasPermi('mall:feedback:export')")
    @Log(title = "用户管理>用户反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody MallFeedback mallFeedback) {
        LambdaQueryWrapper<MallFeedback> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(mallFeedback.getMobile())) {
            wrapper.eq(MallFeedback::getMobile, mallFeedback.getMobile());
        }
        if (mallFeedback.getStatus() != null) {
            wrapper.eq(MallFeedback::getStatus, mallFeedback.getStatus());
        }
        if (mallFeedback.getUserId() != null) {
            wrapper.eq(MallFeedback::getUserId, mallFeedback.getUserId());
        }
        if (mallFeedback.getCreateTime() != null) {
            wrapper.gt(MallFeedback::getCreateTime, mallFeedback.getCreateTime());
        }
        if (mallFeedback.getUpdateTime() != null) {
            wrapper.gt(MallFeedback::getUpdateTime, mallFeedback.getUpdateTime());
        }
        if (mallFeedback.getEndTime() != null) {
            wrapper.lt(MallFeedback::getCreateTime, mallFeedback.getEndTime());
        }
        if (mallFeedback.getStartTime() != null) {
            wrapper.gt(MallFeedback::getCreateTime, mallFeedback.getStartTime());
        }
        if (mallFeedback.getReplyEndTime() != null) {
            wrapper.lt(MallFeedback::getReplyTime, mallFeedback.getReplyEndTime());
        }
        if (mallFeedback.getReplyStartTime() != null) {
            wrapper.gt(MallFeedback::getReplyTime, mallFeedback.getReplyStartTime());
        }
        wrapper.orderByDesc(MallFeedback::getCreateTime);
        Page<MallFeedback> page =  new Page<>(1, Integer.MAX_VALUE);
        Page<MallFeedback> page1 = mallFeedbackService.page(page, wrapper);
        page1.getRecords().forEach(item ->{
            item.setFeedbackTime(item.getCreateTime());
        });
        ExcelUtil<MallFeedback> util = new ExcelUtil<MallFeedback>(MallFeedback.class);

        return util.exportExcel(page1.getRecords(), "意见反馈列表");
    }

    /**
     * 获取意见反馈详细信息
     */
    @ApiOperation(value="获取意见反馈详细信息")
    @PreAuthorize("@ss.hasPermi('mall:feedback:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallFeedback> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallFeedbackService.selectMallFeedbackById(id));
    }

    /**
     * 新增意见反馈
     */
    @ApiOperation(value="新增意见反馈")
    @PreAuthorize("@ss.hasPermi('mall:feedback:add')")
    @Log(title = "用户管理>用户反馈", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallFeedback mallFeedback) {
        return to(mallFeedbackService.insertMallFeedback(mallFeedback));
    }

    /**
     * 修改意见反馈
     */
    @ApiOperation(value="修改意见反馈")
    @PreAuthorize("@ss.hasPermi('mall:feedback:edit')")
    @Log(title = "用户管理>用户反馈", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallFeedback mallFeedback) {
        return to(mallFeedbackService.updateMallFeedback(mallFeedback));
    }

    /**
     * 删除意见反馈
     */
    @ApiOperation(value="删除意见反馈")
    @PreAuthorize("@ss.hasPermi('mall:feedback:remove')")
    @Log(title = "用户管理>用户反馈", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallFeedbackService.deleteMallFeedbackByIds(ids));
    }

    /**
     * 意见反馈统计
     */
    @ApiOperation(value="意见反馈统计")
    @PreAuthorize("@ss.hasPermi('mall:feedback:list')")
    @GetMapping(value = "/queryFeedBackStatistics")
    public HttpResult<FeedBackStatisticsVO> queryFeedBackStatistics() {
        return HttpResult.ok(mallFeedbackService.queryFeedBackStatistics());
    }

}
